import json
import os

DEFAULT_CONFIG = {
    "fw_version": {"enabled": True, "value": ""},
    "device_id": {"enabled": True, "value": ""},
    "fw_res_version": {"enabled": True, "value": ""},
    "g_sensor_id": {"enabled": True, "value": ""},
    "flash_id": {"enabled": True, "value": ""},
    "get_sn": {"enabled": True, "value": ""},
    "get_mac": {"enabled": True, "value": ""},
    "gps_version": {"enabled": True, "value": ""}
}

class ConfigManager:
    def __init__(self, config_file="test_settings.json"):
        self.config_file = config_file
        self.config = self.init_config()

    def init_config(self):
        try:
            with open(self.config_file, 'r', encoding='utf-8') as f:
                config = json.load(f)
                if not all(key in config for key in DEFAULT_CONFIG.keys()):
                    raise ValueError("Invalid configuration format")
                return config
        except (FileNotFoundError, json.JSONDecodeError, ValueError):
            return DEFAULT_CONFIG

    def save_config(self):
        with open(self.config_file, 'w', encoding='utf-8') as f:
            json.dump(self.config, f, indent=4, ensure_ascii=False)

    def get_config(self):
        return self.config

    def update_config(self, key, enabled, value):
        self.config[key]["enabled"] = enabled
        self.config[key]["value"] = value
        self.save_config()
